package com.cl.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.cl.pojo.Vaccines;
import com.cl.service.impl.VaccinesServiceImpl;
import com.cl.utils.MsgUtil;
import com.cl.utils.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

@RestController
public class VaccinesController {
    @Autowired
    private VaccinesServiceImpl vaccinesService;
    @Autowired
    private MsgUtil msgUtil;
    @Autowired
    private RedisUtil redisUtil;
    @GetMapping("/getAllVaccines")
    public String getAllVaccines(@RequestParam("page") int page, @RequestParam("limit") int limit){
        int i = vaccinesService.countAll();
        List<Vaccines> vaccines = vaccinesService.queryAllLimit((page-1)*limit,limit);
        HashMap<String, Object> map = new HashMap<>();
        map.put("count",i);
        map.put("code",0);
        map.put("msg","");
        map.put("data",vaccines);
        return JSON.toJSONString(map);
    }
    @PostMapping("/delete")
    public String delete(@RequestParam("vNo")String vno){
        int vNo = Integer.parseInt(vno);
        int i = vaccinesService.deleteByVno(vNo);
        if (i>0){
            return JSON.toJSONString(msgUtil.success("删除成功"));
        }
        return JSON.toJSONString(msgUtil.fail("删除失败"));
    }
    @PostMapping("/update")
    public String update(@RequestParam("form")String form,@RequestParam("vNo")String vno){
        int vNo = Integer.parseInt(vno);
        JSONObject jsonObject = JSON.parseObject(form);
        String name = (String) jsonObject.get("name");
        String position = (String) jsonObject.get("position");
        String way = (String) jsonObject.get("way");
        String dose = (String) jsonObject.get("dose");
        int i = vaccinesService.updateByVno(vNo, name, position, way, dose);
        if (i>0){
            return JSON.toJSONString(msgUtil.success("修改成功"));
        }
        return JSON.toJSONString(msgUtil.fail("修改失败"));
    }
    @PostMapping("/add")
    public String add(@RequestParam("form")String form){
        JSONObject jsonObject = JSON.parseObject(form);
        String name = (String) jsonObject.get("vName");
        String position = (String) jsonObject.get("vPosition");
        String way = (String) jsonObject.get("vWay");
        String dose = (String) jsonObject.get("vDose");
        int add = vaccinesService.add(name, position, way, dose);
        if (add>0){
            redisUtil.del("vaccines");
            return JSON.toJSONString(msgUtil.success("添加成功"));
        }
        return JSON.toJSONString(msgUtil.fail("添加失败"));
    }
    @GetMapping("/record")
    public String record(){
        int all = vaccinesService.countAll();
        List<String> name = new ArrayList<>();
        List<List<Integer>> number = new ArrayList<>();
        for (int i = 1; i <= all ; i++) {
            List<Integer> list = new ArrayList<>();
            List<Map<String, Object>> maps  = vaccinesService.queryRecord(i);
            if (maps.size()!=0){
                Iterator<Map<String, Object>> iterator = maps.iterator();
                String v_name = (String) iterator.next().get("v_name");
                name.add(v_name);
                for (Map<String, Object> map : maps) {
                    list.add((Integer) map.get("i_number"));
                }
                number.add(list);
            }
        }
        HashMap<String, List> map = new HashMap<>();
        map.put("name",name);
        map.put("number",number);
        return JSON.toJSONString(map);
    }
}
